﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;

public partial class admin_Quanlytuvan : System.Web.UI.Page
{
    TuVanController TuVanCTR = new TuVanController();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session["Employee"] == null)
            Response.Redirect("login.aspx");
        if (!IsPostBack)
        {
            if(Request["tuvantk"]==null)
                gvDanhSachTuvan.DataSource = TuVanCTR.fnC_GetAllDataTuVan();
            else
            {
                hdtimkiem.Value=Request["tuvantk"].ToString();
                string timkiem = "%" + hdtimkiem.Value + "%";
                gvDanhSachTuvan.DataSource = TuVanCTR.fnC_TimKiemTuVan(timkiem);
            }
            DataBind();
        }
    }
    protected void gvDanhSachTuvan_SelectedIndexChanged(object sender, EventArgs e)
    {
        HiddenField idtv = (HiddenField)gvDanhSachTuvan.SelectedRow.Cells[7].FindControl("hdidtv");
        Response.Redirect("capnhattuvan.aspx?update=true&&idtv=" + idtv.Value);
    }
    protected void gvDanhSachTuvan_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        gvDanhSachTuvan.PageIndex = e.NewPageIndex;
        if (hdtimkiem.Value == "")
            gvDanhSachTuvan.DataSource = TuVanCTR.fnC_GetAllDataTuVan();
        else
        {
            string timkiem = "%" + hdtimkiem.Value + "%";
            gvDanhSachTuvan.DataSource = TuVanCTR.fnC_TimKiemTuVan(timkiem);
        }
        DataBind();
    }
    protected void gvDanhSachTuvan_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            ImageButton btn = (ImageButton)e.Row.Cells[6].Controls[0];
            //khi click vao nut xoa se phat sinh doan ma javascript thong bao xac nhan co muon xoa hay khong
            btn.OnClientClick = "if(!confirm('Bạn có muốn xóa tư vấn :<" + Server.HtmlDecode(e.Row.Cells[1].Text) + ">  không?'))return false";
        }
    }
    protected void gvDanhSachTuvan_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {

        TuVanEntity TuVanE = new TuVanEntity();
        HiddenField idtv = (HiddenField)gvDanhSachTuvan.Rows[e.RowIndex].Cells[7].FindControl("hdidtv");
        TuVanE.Idtv = int.Parse(idtv.Value);
        TuVanE = TuVanCTR.fnG_GetTuVan(idtv.Value);
        TuVanCTR.fnC_DeleteTuVan(TuVanE);
        File.Delete(Server.MapPath("~/images/TuVan/") + TuVanE.HinhDaiDien + ".jpg");
        gvDanhSachTuvan.DataSource = TuVanCTR.fnC_GetAllDataTuVan();
        DataBind();
    }
    protected void ImageActive_Click(object sender, ImageClickEventArgs e)
    {
        ImageButton imbtactive = (ImageButton)sender;
        TuVanEntity TuVanE = new TuVanEntity();
        TuVanE.Idtv = int.Parse(imbtactive.CommandArgument);
        TuVanCTR.fnC_ActiveTuVan(TuVanE);
    }
    protected void imgbtsearch_Click(object sender, ImageClickEventArgs e)
    {
        DateTime tungay = DateTime.Parse(txttungay.Text);
        DateTime denngay = DateTime.Parse(txtdenngay.Text);
        gvDanhSachTuvan.DataSource = TuVanCTR.fnC_GetDataForDate(tungay, denngay);
        DataBind();
    }
}